﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xarmarin.GoldFinger.Engine.Core;
using Xarmarin.GoldFinger.Engine.Rand;
using Xarmarin.GoldFinger.Interface;

namespace Xarmarin.GoldFinger.Engine.WorldBase
{
    //下（1，2，3）中（4，5，6）上（7，8，9）大圆满（10） 阶
    //练气、筑基、金丹、元婴、化神；（金丹概率渡劫、元婴需渡劫）
    //练虚、合体、大乘
    //渡劫（3，6，9重），飞升（小、大、仙劫），
    //失败后转为地仙，散仙，鬼仙（1-10重，每层需渡劫）或夺舍

    //成功后飞升仙界
    //玄仙，金仙，太乙，大罗，
    //法则道主（每个法则唯一）

    //境界晋级有瓶颈
    public class StateBase
    {
        /// <summary>
        /// 突破小境界，小境界到达极限会突破大境界
        /// </summary>
        public static Task MoveNextSmallState(Status status = Status.Common)
        {
            if (CheckStatus(status))
            {
                long NextSmallState = ((SmallState)KzContext.CurrSubject.SmallState).GetNextValue();
                switch (NextSmallState)
                {
                    //突破小境界
                    case -9999:
                        MoveNextSimpleState(status);
                        break;
                    //到达大圆满境界
                    case 10:
                        KzContext.IsDzogchen = true;
                        KzContext.CurrSubject.SmallState = NextSmallState;
                        KzContext.CurrSubject.SmallStateStr = ((SmallState)NextSmallState).GetDescription();
                        DependencyService.Get<IToast>().LongAlert("" + KzContext.CurrSub + " 修行有成，气脉流转生生不息，已是到达" + KzContext.CurrSubject.StateStr + KzContext.CurrSubject.SmallStateStr + "了");
                        break;
                    case 0:
                        KzContext.CurrSubject.SmallState = (long)SmallState.Inferior;
                        KzContext.CurrSubject.SmallStateStr = SmallState.Inferior.GetDescription();
                        DependencyService.Get<IToast>().ShortAlert("天有不测风云， " + KzContext.CurrSub + " 运功岔脉，气血倒涌之下，跌落到了" + KzContext.CurrSubject.StateStr + KzContext.CurrSubject.SmallStateStr);
                        break;
                    //正常突破
                    default:
                        KzContext.CurrSubject.SmallState = NextSmallState;
                        KzContext.CurrSubject.SmallStateStr = ((SmallState)NextSmallState).GetDescription();
                        DependencyService.Get<IToast>().LongAlert("天道有感 " + KzContext.CurrSub + " 修行有成，突破至" + KzContext.CurrSubject.StateStr + KzContext.CurrSubject.SmallStateStr);
                        break;
                }
            }
            return Task.CompletedTask;

        }
        /// <summary>
        /// 突破大境界
        /// </summary>
        public static void MoveNextSimpleState(Status status = Status.Common)
        {
            //小境界恢复初始
            KzContext.CurrSubject.SmallState = (long)SmallState.Inferior;
            KzContext.CurrSubject.SmallStateStr = SmallState.Inferior.GetDescription();
            long NextState = ((SimpleState)KzContext.CurrSubject.State).GetNextValue();
            switch (NextState)
            {
                //突破小境界
                case -9999:
                    KzContext.CurrSubject.SmallState = (long)SmallState.Dzogchen;
                    KzContext.CurrSubject.SmallStateStr = "極";
                    DependencyService.Get<IToast>().LongAlert(KzContext.CurrSub + " 悟得天地真谛，已然超脱凡人，半步踏入仙境，却是寻不得《引仙台》飞升仙界");
                    break;
                //到达大圆满境界
                case 10:
                    KzContext.IsDzogchen = true;
                    KzContext.CurrSubject.State = NextState;
                    KzContext.CurrSubject.StateStr = ((SimpleState)NextState).GetDescription();
                    DependencyService.Get<IToast>().LongAlert("" + KzContext.CurrSub + " 修行有成，气脉流转生生不息，已是到达" + KzContext.CurrSubject.StateStr + KzContext.CurrSubject.SmallStateStr + "了");
                    break;
                //正常突破
                default:
                    KzContext.CurrSubject.State = NextState;
                    KzContext.CurrSubject.StateStr = ((SimpleState)NextState).GetDescription();
                    DependencyService.Get<IToast>().LongAlert("天道有感 " + KzContext.CurrSub + " 修行有成，突破至" + KzContext.CurrSubject.StateStr + KzContext.CurrSubject.SmallStateStr);
                    break;
            }
        }

        /// <summary>
        /// 查看状态是否可以突破
        /// </summary>
        public static bool CheckStatus(Status status)
        {
            var isOk = true;
            var msg = "修为不足，无法突破！";
            //判断修为
            if (KzContext.CurrSubject.Mana<KzContext.CurrSubject.NextMana)
            {
                isOk = false;
            }
            //判断机率
            if (GetThisProb()<GetCurrentProbability())
            {
                msg ="天有不测风云， " + KzContext.CurrSub + " 运功岔脉，气血倒涌之下，未能成功突破境界！";
                isOk = false;
            }

            //判断心魔

            //判断天劫

            //判断是否需要外物

            //突破失败
            if (!isOk)
            {
                DependencyService.Get<IToast>().ShortAlert(msg);
            }
            //成功突破
            if (isOk)
            {
                //减去突破所需修为
                KzContext.CurrSubject.Mana -= KzContext.CurrSubject.NextMana;
                //下级突破修为
                var m = KzContext.CurrSubject.Savvy + KzContext.CurrSubject.Mind + KzContext.CurrSubject.Physique + KzContext.CurrSubject.Lucy;
                KzContext.CurrSubject.NextMana +=(long)(RandCore.GetRandValue((int)Math.Pow(10, m.ToString().Length), 
                    Convert.ToInt32(Math.Pow(10, m.ToString().Length+KzContext.CurrSubject.SmallState))) /m);
            }
            return isOk;
        }
        /// <summary>
        /// 获取突破机率
        /// </summary>
        /// <returns></returns>
        private static long GetCurrentProbability()
        {
            long ret = 10;
            //

            return ret;
        }
        private static long GetThisProb()
        {
            //气运X （因果+体魄+神魂+悟性）
            return (long)(KzContext.CurrSubject.Lucy * (KzContext.CurrSubject.Savvy * 3 + KzContext.CurrSubject.Physique * 4 + KzContext.CurrSubject.Mind * 5));
        }
    }
}
